package com.zhiwang.xiaoqu.mvp.model.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase

@Database(
    entities = [

        User::class
    ],
    version = 2,
    exportSchema = false
)
abstract class AppDataBase : RoomDatabase() {

    abstract fun userDao(): UserDao

//    abstract fun userDao(): UserDao
//
//    abstract fun downLoadApkDao():DownLoadApkDao

    companion object SOUL {
        private const val DATABASE_NAME = "AppScaffoldDataBase"
        @Volatile
        var INSTANCE: AppDataBase? = null

        fun getInstance(context: Context): AppDataBase {
            return INSTANCE
                ?: synchronized(SOUL) {
                    return INSTANCE
                        ?: buildDatabase(context).also { INSTANCE = it }
                }
        }

        private fun buildDatabase(context: Context): AppDataBase {
            return Room.databaseBuilder(context, AppDataBase::class.java, DATABASE_NAME)
                .allowMainThreadQueries()
                .addMigrations(SampleMigration(1, 2))
                .addCallback(object : Callback() {
                    override fun onCreate(db: SupportSQLiteDatabase) {
                        super.onCreate(db)
                        //do some work like read local data ..etc
                    }
                })
                .build()
        }
    }


}